<template>
  <div>
    <el-upload
      class="avatar-uploader"
      :action="action"
      :multiple="false"
      :limit="1"
      :show-file-list="true"
      :on-success="onSuccess"
      :on-error="onError"
      :on-exceed="onExceed"
      :on-remove="onRemove"
      :on-preview="onPreview"
      :disabled="disabled"
      :headers="headers"
      :file-list="fileList"
    >
      <!-- :before-upload="beforeUpload" -->
      <img v-if="fileList[0]" :src="fileList[0].url" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { deleteImage } from "@/api/image";
export default {
  props: {
    fileList: {
      type: Array,
      default: [],
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      action: `${process.env.VUE_APP_BASE_API}/admin/image`,
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  computed: {
    headers() {
      return {
        Authorization: `${getToken()}`,
      };
    },
  },
  methods: {
    // 上传成功
    onSuccess(response, file, fileList) {
      const { code, msg, data } = response;
      if (code !== 1) {
        this.$message({
          message: (msg && `上传失败，失败原因：${msg}`) || "上传失败",
          type: "error",
        });
        this.$emit("onError", data);
      } else {
        this.$message({
          message: "上传成功",
          type: "success",
        });
        this.$emit("onSuccess", data);
      }
    },
    // 上传失败
    onError(err) {
      const errMsg = (err.message && JSON.parse(err.message)) || "上传失败";
      this.$message({
        message:
          (errMsg.msg && `上传失败，失败原因：${errMsg.msg}`) || "上传失败",
        type: "error",
      });
      this.$emit("onError", err);
    },
    // 限制上传的条数
    onExceed() {
      this.$message({
        message: "每次只能上传一张图片",
        type: "warning",
      });
    },
    // 预览图片
    onPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 移出
    onRemove(file) {
      console.log(file);
      deleteImage(file.name).then((response) => {
        if (response.code == 1) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.$emit("onRemove");
        }
      });
    },
    // 上传前校验
    beforeUpload(file) {
      const isJPG = file.type === "image/jpeg/gif/jpg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>